
//
// 120120 - AH - Created.
// 120224 - AH - (Inside) Implemented new Ontology method 
//               getSoekratNames(List<SoekratNames>).
//

package org.soekrat.engine.wheel.ontology;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.soekrat.AttributeObject;
import org.soekrat.engine.SoekratName;
import org.soekrat.engine.ontology.OntologyDatastructure;
import org.soekrat.engine.ontology.ptable.Product;
import org.soekrat.engine.ontology.ptable.ProductTable;
import org.soekrat.engine.retrieval.ptable.ProductScan;
import org.soekrat.engine.visitor.Input;
import org.soekrat.engine.wheel.Wheel;
import org.soekrat.engine.wheel.retrieval.RetrievalEvent;
import org.soekrat.engine.wheel.retrieval.RetrievalEventSource;
import org.soekrat.engine.wheel.retrieval.RetrievalEventType;

/**
 * <p>
 *   {@link Ontology} based on
 *   {@link ProductTable}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class ProductTableOntology extends Ontology 
{
  private final static RetrievalEventType c_pass=RetrievalEventType.PASS;
  
  private final RetrievalEventType m_step=RetrievalEventType.STEP;
  
  private final RetrievalEventSource m_source=
    RetrievalEventSource.PRODUCT_TABLE;
  
  private final ProductTable m_ptable;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param wheel  {@link Wheel}-.
   * @param ptable {@link ProductTable}.
   */
  public ProductTableOntology(Wheel wheel, ProductTable ptable)
  { 
    super(wheel);
    
    if (ptable==null)
    {
      throw new NullPointerException("ptable");
    }
    
    m_ptable=ptable;
  }
  
  /**
   * <p>
   *   Gets 
   *   {@link ProductTable}.
   * </p>
   * @return {@code ProductTable}.
   */
  public ProductTable getProductTable(){ return m_ptable; }
  
  @Override
  public List<SoekratName> getSoekratNames(List<SoekratName> names) 
  {
    names=(names==null) ? new ArrayList<SoekratName>() : names;
    
    int n=m_ptable.getRowFactorCount();
    for (int i=0; i<n; i++)
      names.add(m_ptable.getRowFactor(i).getSoekratName());
    
    n=m_ptable.getColFactorCount();
    for (int i=0; i<n; i++)
      names.add(m_ptable.getColFactor(i).getSoekratName());
    
    for (Product curr : m_ptable) names.add(curr.getSoekratName());
    
    return names;
  }

  @Override
  public String getName(){ return "Product Table Ontology"; }

  @Override
  public OntologyDatastructure getOntologyDatastructure()
  {
    return m_ptable;
  }

  @Override
  protected Object scan(Input keywords) 
  {
    ProductScan ps=new ProductScan(m_ptable, keywords);
    return ps;
    /*
    Set<Product> products=ps.getProductsOfMatchFactors();
    
    System.out.println("Product Table Ontology scan matched; "+products+
      " for input: "+keywords);
    
    return products;
    */
  }

  @Override
  protected RetrievalEvent paint(Object scan, List<AttributeObject> paint)
  {
    ProductScan ps=(ProductScan)scan;
    
    if (ps.noMatch()) 
    {
      return new RetrievalEvent("mine pass (case no match)", 
        c_pass, m_source);
    }
    
    RetrievalEvent retVal=new RetrievalEvent("ProductTable", m_step, m_source);
    
    Set<Product> s=ps.getProductsOfMatchFactors();
    for (Product product : s) paint.add(product);
    
    return retVal;
  }

  @Override
  public Input getTokens(List<AttributeObject> paint) 
  {
    if (paint==null)
    {
      throw new NullPointerException("paint");
    }
    
    Input retVal=new Input();
    
    for (AttributeObject curr : paint)
    {
      if (curr instanceof Product) 
        retVal.add(((Product) curr).getSoekratName().getPure());    
    }
    
    return retVal;
  }

}
